package com.inspur.sc.springboot.service.impl;

import com.inspur.sc.springboot.dao.SysPermissionGroupDao;
import com.inspur.sc.springboot.entity.SysPermissionGroup;
import com.inspur.sc.springboot.service.SysPermissionGroupService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Slf4j
@Service
public class SysPermissionGroupServiceImpl implements SysPermissionGroupService {

    @Autowired
    private SysPermissionGroupDao permissionGroupDao;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean save(Integer groupId, List<Integer> menuNodes,List<Integer> permissionNodes) {
        try{
            permissionGroupDao.deleteBySysGroupId(groupId);
            if(!menuNodes.isEmpty()){
                menuNodes.forEach(permissionId->{
                    SysPermissionGroup permissionGroup=new SysPermissionGroup();
                    permissionGroup.setSysGroupId(groupId);
                    permissionGroup.setSysPermissionId(permissionId);
                    permissionGroup.setType("m");
                    permissionGroupDao.save(permissionGroup);
                });
            }

            if(!permissionNodes.isEmpty()){
                permissionNodes.forEach(permissionId->{
                    SysPermissionGroup permissionGroup=new SysPermissionGroup();
                    permissionGroup.setSysGroupId(groupId);
                    permissionGroup.setSysPermissionId(permissionId);
                    permissionGroup.setType("p");
                    permissionGroupDao.save(permissionGroup);
                });
            }


            return true;
        }catch (Exception e){
            log.error(e.getMessage());
            return false;
        }
    }

    @Override
    public List<SysPermissionGroup> findBySysGroupId(Integer groupId) {
        return permissionGroupDao.findBySysGroupId(groupId);
    }
}
